package com.chen10.gobang.controller;

import com.chen10.gobang.common.enums.response.ErrorEnum;
import com.chen10.gobang.common.enums.response.SuccessMsgEnum;
import com.chen10.gobang.common.exception.BusinessException;
import com.chen10.gobang.common.protocal.ResponseResult;
import com.chen10.gobang.common.protocal.ResponseVO;
import com.chen10.gobang.model.dto.HandleFriendRequestDTO;
import com.chen10.gobang.model.dto.SendFriendRequestDTO;
import com.chen10.gobang.model.vo.FriendRequestListVO;
import com.chen10.gobang.service.FriendRequestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/friend/request")
@Slf4j
public class FriendRequestController {
    @Resource
    private FriendRequestService friendRequestService;

    /**
     * 获取好友请求列表
     * @param userId
     * @return
     */
    @GetMapping("/list/{userId}")
    public ResponseVO<List<FriendRequestListVO>> getFriendRequestList(@PathVariable Integer userId) {
        log.info("获取好友请求列表");
        try {
            return ResponseResult.success(SuccessMsgEnum.GET_FRIEND_REQUEST_LIST_SUCCESS.getMsg(),
                    friendRequestService.getFriendRequestList(userId));
        } catch (BusinessException e) {
            return ResponseResult.error(e.getMessage());
        }
    }

    /**
     * 处理好友请求
     * @param handleFriendRequestDTO
     * @return
     */
    @PostMapping("/handle")
    public ResponseVO handleFriendRequest(@RequestBody HandleFriendRequestDTO handleFriendRequestDTO) {
        log.info("处理好友请求,sendFriendRequestDTO:{}", handleFriendRequestDTO);
        try {
            int code = friendRequestService.handleFriendRequest(handleFriendRequestDTO);
            if(code == ErrorEnum.ALREADY_FRIEND.getCode()){
                throw new BusinessException(ErrorEnum.ALREADY_FRIEND);
            }
            return ResponseResult.success(SuccessMsgEnum.HANDLE_FRIEND_REQUEST_SUCCESS);
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(),e.getMessage());
        }
    }

    /**
     * 发送好友请求
     *
     * @param sendFriendRequestDTO
     * @return
     */
    @PostMapping("/send")
    public ResponseVO sendFriendRequest(@RequestBody SendFriendRequestDTO sendFriendRequestDTO) {
        log.info("发送好友请求,sendFriendRequestDTO:{}", sendFriendRequestDTO);
        try {
            friendRequestService.sendFriendRequest(sendFriendRequestDTO);
            return ResponseResult.success(SuccessMsgEnum.SEND_FRIEND_REQUEST_SUCCESS);
        } catch (BusinessException e) {
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }
}
